//
//  HNSH_GroupApplyViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/18.
//

#import "HNSH_GroupApplyViewController.h"

//view
#import "HNSH_GroupApplyCell.h"

@interface HNSH_GroupApplyViewController ()
@property (nonatomic, strong)NSMutableArray * dataArray;
@end

static NSString *HNSH_GroupApplyCellID = @"HNSH_GroupApplyCellID";

@implementation HNSH_GroupApplyViewController

//MARK: Cycle Methods
- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self showNavTitle:@"邀请进群申请" showLeftItem:YES];
        
    [self configTableView];
    
    [self loadNewData];
}

- (void)configTableView {
    self.tableView.backgroundColor = [UIColor appWhiteLabelColor];
    [self.tableView registerClass:[HNSH_GroupApplyCell class] forCellReuseIdentifier:HNSH_GroupApplyCellID];
    self.tableView.rowHeight = 72;
}

- (void)loadNewData{
    [HNSH_ChatNetServer requestRongCloudGroupApplyListWithGroupId:self.targetId success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            NSArray * list = responseObject.resultset[@"list"];
            self.dataArray = [NSMutableArray arrayWithArray:[NSArray yy_modelArrayWithClass:HNSH_GroupApplyModel.class json:list]];
            [self.tableView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//MARK: 接受
- (void)acceptWithCell:(HNSH_GroupApplyCell *)cell{
    NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
    HNSH_GroupApplyModel * model = self.dataArray[indexPath.row];
    [HNSH_ChatNetServer requestRongCloudGroupAgreeWithRcUserId:model.uid success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [self.view makeToast:@"添加成功"];
            [self loadNewData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//MARK: Getter Methods
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [@[] mutableCopy];
    }
    return _dataArray;
}

//MARK: UITableViewDelegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HNSH_GroupApplyCell * cell = [tableView dequeueReusableCellWithIdentifier:HNSH_GroupApplyCellID];
    cell.model = self.dataArray[indexPath.row];
    WeakSelf(weakSelf);
    cell.acceptBlock = ^(HNSH_GroupApplyCell * _Nonnull selCell) {
        [weakSelf acceptWithCell:selCell];
    };
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
@end
